package com.dongdongqiang.common.utils;

import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * String类型转成各种各样基本类型的方法
 * Created by zhangzhenbin on 17-3-7.
 */
public class StringToTTypeUtil {
    /**
     * 字符串转换为基本类型
     * 支持 int,char,long,byte,boolean,float,double
     * Integer,Character,Long,Byte,Boolean,Float,Double,String,BigDecimal,Date,Timestamp,sql.Date
     */
    public static <T> T convert(String s, Class<T> clazz) {
        try {
            if (clazz == int.class || clazz == Integer.class) {
                return (T) Integer.class.cast(NumberUtils.toInt(s, 0));
            } else if (clazz == char.class || clazz == Character.class) {
                return (T) Character.class.cast(CharUtils.toChar(s, (char) 0x0));
            } else if (clazz == long.class || clazz == Long.class) {
                return (T) Long.class.cast(NumberUtils.toLong(s, 0L));
            } else if (clazz == byte.class || clazz == Byte.class) {
                return (T) Byte.class.cast(NumberUtils.toByte(s, (byte) 0x0));
            } else if (clazz == boolean.class || clazz == Boolean.class) {
                return (T) Boolean.class.cast(Misc.toBoolean(s));
            } else if (clazz == float.class || clazz == Float.class) {
                return (T) Float.class.cast(NumberUtils.toFloat(s, 0f));
            } else if (clazz == double.class || clazz == Double.class) {
                return (T) Double.class.cast(NumberUtils.toDouble(s, 0d));
            } else if (clazz == String.class) {
                return clazz.cast(s);
            } else if (clazz == BigDecimal.class) {
                return clazz.cast(new BigDecimal(s));
            } else if (clazz == Date.class) {
                return clazz.cast(DateUtil.stringForceToDate(s));
            } else if (clazz == java.sql.Date.class) {
                return clazz.cast(java.sql.Date.valueOf(s));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    private static <T> T testConvent(String s, Class<T> clazz) {
        if (clazz == int.class) {
            return (T) Integer.class.cast(NumberUtils.toInt(s, 0));
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("test start ----");
        System.out.println("int");
        System.out.println(convert("1", int.class));
        System.out.println(convert("1", Integer.class));
        System.out.println("char");
        System.out.println(convert("32", char.class));
        System.out.println(convert("32", Character.class));
        System.out.println("long");
        System.out.println(convert("32", long.class));
        System.out.println(convert("32", Long.class));
        System.out.println("byte");
        System.out.println(convert("32", byte.class));
        System.out.println(convert("32", Byte.class));
        System.out.println("boolean");
        System.out.println(convert("true", boolean.class));
        System.out.println(convert("true", Boolean.class));
        System.out.println("float");
        System.out.println(convert("1.1", float.class));
        System.out.println(convert("1.1", Float.class));
        System.out.println("double");
        System.out.println(convert("1.1", double.class));
        System.out.println(convert("1.1", Double.class));
        System.out.println("BigDecimal");
        System.out.println(convert("11.123", BigDecimal.class));
        System.out.println("Date");
        System.out.println(convert("2017-3-7 10:00:00", Date.class));
        System.out.println("sql.Date");
        System.out.println(convert("2017-3-7", java.sql.Date.class));
        System.out.println("String");
        System.out.println(convert("sdafdsaf", String.class));
    }
}
